package algorithm.leetcode.simple;

/**
 * 将一个给定字符串根据给定的行数，以从上往下、从左到右进行 Z 字形排列。
 *
 * @author jack.wu
 * @version 1.0
 * @date 2020/8/17
 */
public class Question6 {

    public String convert(String s, int numRows) {
        if (s == null || s.length() == 0 || numRows <= 1) {
            return s;
        }
        StringBuilder[] sbArray = new StringBuilder[numRows];
        for (int i = 0; i < numRows; i++) {
            sbArray[i] = new StringBuilder();
        }
        int index = 0;
        boolean down = false;
        for (int j = 0; j < s.length(); j++) {
            char c = s.charAt(j);
            if (index == 0 || index == numRows - 1) {
                down = !down;
            }
            sbArray[index].append(c);
            index += down ? 1 : -1;
        }
        StringBuilder result = new StringBuilder();
        for (StringBuilder sb : sbArray) {
            result.append(sb.toString());
        }
        return result.toString();
    }


    public static void main(String[] args) {
        Question6 z = new Question6();
        String s = "LEETCODEISHIRING";
        int numRows = 3;
        String convertStr = z.convert(s, numRows);
        System.out.println(convertStr);
    }
}
